package com.gimi.cloud.bbp.manage.controller.mall;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.enums.ServiceNameConstants;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.mall.MallAppBannerGroupDTO;
import com.gimi.cloud.bbp.dto.mall.MallAppColumnDTO;
import com.gimi.cloud.bbp.dto.mall.MallAppRecommentGroupDTO;
import com.gimi.cloud.bbp.dto.mall.MallAppSpecialGroupDTO;
import com.gimi.cloud.bbp.manage.service.mall.MallAppBannerGroupService;
import com.gimi.cloud.bbp.manage.service.mall.MallAppColumnService;
import com.gimi.cloud.bbp.manage.service.mall.MallAppRecommentGroupService;
import com.gimi.cloud.bbp.manage.service.mall.MallAppSpecialGroupService;
import com.github.pagehelper.PageInfo;

/**
 * 栏目相关Service
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-01-30
 **/

@RestController
@RequestMapping("/mall/column")
public class MallAppColumnController {
    private static final Logger logger = LoggerFactory.getLogger(MallAppColumnController.class);
    private MallAppColumnService mallAppColumnService;
    private MallAppBannerGroupService mallAppBannerGroupService;
    private MallAppSpecialGroupService mallAppSpecialGroupService;
    private MallAppRecommentGroupService mallAppRecommentGroupService;

    @Autowired
    public MallAppColumnController(MallAppColumnService mallAppColumnService, MallAppBannerGroupService mallAppBannerGroupService, MallAppSpecialGroupService mallAppSpecialGroupService, MallAppRecommentGroupService mallAppRecommentGroupService) {
        this.mallAppColumnService = mallAppColumnService;
        this.mallAppBannerGroupService = mallAppBannerGroupService;
        this.mallAppSpecialGroupService = mallAppSpecialGroupService;
        this.mallAppRecommentGroupService = mallAppRecommentGroupService;
    }

    /**
     * 获取Banner列表
     * @param request
     * @param response
     * @param dto 查询参数
     * @throws Exception
     */
    @RequestMapping("/list")
    public Response list(HttpServletRequest request, HttpServletResponse response,@RequestBody MallAppColumnDTO dto) throws Exception{
        Response resp = new Response();
        List<MallAppColumnDTO> appColumnList = mallAppColumnService.queryList(dto,true);
        PageInfo<MallAppColumnDTO> pageInfo = new PageInfo<>(appColumnList);
        Map<String,Object> resData = new HashMap<>();
        resData.put(ServiceNameConstants.PAGEDATA_KEY,pageInfo);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return resp;
    }

    /**
     * 获取单一的Banner信息
     * @param request
     * @param response
     * @param dto
     * @throws Exception
     */
    @RequestMapping("/info")
    public Response info(HttpServletRequest request,HttpServletResponse response,@RequestBody MallAppColumnDTO dto) throws Exception{
        Response resp = new Response();
        MallAppColumnDTO appColumn = mallAppColumnService.load(dto,dto.getAclId());
        Map<String,Object> resData = new HashMap<>();
        resData.put("appColumn",appColumn);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"获取成功");
        return resp;
    }

    /**
     * 新增或编辑栏目信息
     * @param request
     * @param response
     * @param dto 待保存的数据
     * @throws Exception
     */
    @RequestMapping("/saveOrUpdate")
    public Response saveOrUpdate(HttpServletRequest request, HttpServletResponse response,@RequestBody MallAppColumnDTO dto) throws Exception{
        Response resp = new Response();
        mallAppColumnService.saveOrUpdate(dto);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"更新成功");
        return resp;
    }
    /**
     * 删除栏目数据
     * @param request
     * @param response
     * @param dto 待删除的栏目组数据
     * @throws Exception
     */
    @RequestMapping("/delete")
    private Response delete(HttpServletRequest request, HttpServletResponse response, @RequestBody MallAppColumnDTO dto) throws  Exception{
        Response resp = new Response();
        mallAppColumnService.delete(dto,dto.getAclId(),false);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"删除成功");
        return resp;
    }

    /**
     * 根据栏目类型获取组分页数据
     * @param request
     * @param response
     * @param dto 查询参数：queryType   banner:banner,special:专场，recomment，推荐
     * @throws Exception
     */
    @RequestMapping("/detailList")
    public Response detailList(HttpServletRequest request,HttpServletResponse response,@RequestBody MallAppColumnDTO dto) throws Exception{
        Response resp = new Response();
        Map<String,Object> resData = new HashMap<>();
        if("banner".equals(dto.getQueryType())){
            //获取banner组列表
            MallAppBannerGroupDTO mallAppBannerGroup = new MallAppBannerGroupDTO();
            ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(dto,mallAppBannerGroup);
            List<MallAppBannerGroupDTO> mallAppBannerGroups =mallAppBannerGroupService.queryAppBannerGroupAndInitChild(mallAppBannerGroup,true);
            resData.put("bannerGroup",new PageInfo<>(mallAppBannerGroups));
        }else if("special".equals(dto.getQueryType())){
            //获取专场组列表
            MallAppSpecialGroupDTO appSpecialGroup = new MallAppSpecialGroupDTO();
            ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(dto,appSpecialGroup);
            List<MallAppSpecialGroupDTO> mallAppSpecialGroups = mallAppSpecialGroupService.queryAppSpecialGroupAndInitChild(appSpecialGroup,true);
            resData.put("specialGroup",new PageInfo<>(mallAppSpecialGroups));
        }else if("recomment".equals(dto.getQueryType())){
            //获取今日推荐组列表
            MallAppRecommentGroupDTO appRecommentGroup = new MallAppRecommentGroupDTO();
            ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(dto,appRecommentGroup);
            List<MallAppRecommentGroupDTO> mallAppRecommentGroups = mallAppRecommentGroupService.queryAppRecommentGroupAndInitChild(appRecommentGroup,true);
            resData.put("recommentGroups",new PageInfo<>(mallAppRecommentGroups));
        }
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"获取成功");
        resp.setRes_data(resData);
        return resp;
    }


}
